package com.zqc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zqc.common.R;
import com.zqc.entity.User;
import com.zqc.service.UserService;
import com.zqc.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public UserService userService;

    @Autowired
    public RedisTemplate<Object,Object> redisTemplate;


    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session) {
        // 获取手机号
        String phone = user.getPhone();

        if (StringUtils.isNotEmpty(phone)) {
            // 生成随机验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("验证码={}",code);
            // 调用阿里云短信服务发送短信
            // SMSUtils.sendMessage("瑞吉外卖","",phone,code);
            // 保存验证码
            // session.setAttribute(phone, code);

            // 将验证码保存到redis中,过期时间为5分钟
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);

            return R.success("手机验证码短信发送成功");
        }

        return R.success("短信发送失败");
    }

    @PostMapping("/login")
    public R<User> sendMsg(@RequestBody Map map, HttpSession session) {
        // 获取手机号
        String phone = map.get("phone").toString();
        // 获取验证码
        String code = map.get("code").toString();
        // 获取session中的验证码
        // Object codeInSession = session.getAttribute(phone);

        // 从redis中获取验证码
        Object codeInSession = redisTemplate.opsForValue().get(phone);
        // 比较验证码，如果验证码正确，则登录成功
        if (codeInSession != null && codeInSession.equals(code)) {
            // 判断用户是否是新用户，不是则保存到数据库
            User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getPhone, phone));

            if (user == null) {
                user = new User();
                user.setPhone(phone);
                userService.save(user);
            }

            // 将用户id保存到session
            session.setAttribute("user",user.getId());

            // 登录成功，将验证码删除
            redisTemplate.delete(phone);

            return R.success(user);
        }
        return R.error("验证码错误");
    }

    /**
     * 退出登录
     * @return
     */
    @PostMapping("/loginout")
    public R<String> logout(HttpSession session){
        // 清空session
        session.removeAttribute("user");
        return R.success("退出登录");

    }

}
